from machine import Pin, UART, Timer
from machine import Timer
import time
import bluetooth
from car import CAR
from neopixel import NeoPixel

#定义红、绿、蓝三种颜色
RED=(25,0,0)
GREEN=(0,25,0)
LEFT=(0,25,25)
RIGHT= (25,0,25)

BLACK = (0,0,0)
global num
num = 8

#22引脚连接灯带，灯珠数量30
pin = Pin(5, Pin.OUT)
np = NeoPixel(pin, 25)

#设置灯珠颜色，本实验供30个灯珠
def Color_buf(color):
    for i in range(25):
        np[i]=color

def ws(color):
    for i in range(0,10):
        np[num + i] = color
        np.write()
        np[num - i] = color
        np.write()
        np[num + i + 1] = color
        np.write()
        np[num - i - 1] = color
        np.write()
        np[num + i + 2] = color
        np.write()
        np[num - i - 2] = color
        np.write()
        np[num + i + 3] = color
        np.write()
        np[num - i - 3] = color
        np.write()        
        np[num + i] = BLACK
        np.write()        
        np[num - i] = BLACK
        np.write()
        time.sleep(0.2)

buf = bytes(0)
BLE_MSG = ""
uart = UART(1, baudrate=115200, tx=17, rx=16)

if __name__ == "__main__":
    global speed
    speed = 200
    Car = CAR()
    Car.setspeed(speed)

    while True:
        if (uart.any()):
            buf = uart.readline()
            print('Echo Byte: {}'.format(buf))
            angle = buf.decode("utf-8")
            angle = angle.replace("\\n",'')
            x=angle.split(" ",1)
            distance = float(x[0])
            angle = float(x[1])
            Car.setspeed(300)
            Car.diserror(distance,angle)
            Car.angleerror(distance,angle)


